﻿/*
 * Jsofteditor - The text editor for Internet - http://www.jsofteditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * This is a utility object which can be used to load specific components of
 * Jsofteditor, including all dependencies.
 */

var Jsoft_GENERIC = 1 ;
var Jsoft_GENERIC_SPECIFIC = 2 ;
var Jsoft_SPECIFIC = 3 ;

var JsoftScriptLoader = new Object() ;
JsoftScriptLoader.JsofteditorPath = '/jsofteditor/' ;

JsoftScriptLoader._Scripts = new Object() ;
JsoftScriptLoader._LoadedScripts = new Object() ;

JsoftScriptLoader._IsIE = (/msie/).test( navigator.userAgent.toLowerCase() ) ;

JsoftScriptLoader.Load = function( scriptName )
{
	// Check if the script has already been loaded.
	if ( scriptName in JsoftScriptLoader._LoadedScripts )
		return ;

	JsoftScriptLoader._LoadedScripts[ scriptName ] = true ;

	var oScriptInfo = this._Scripts[ scriptName ] ;

	if ( !oScriptInfo )
	{
		alert( 'JsoftScriptLoader: The script "' + scriptName + '" could not be loaded' ) ;
		return ;
	}

	for ( var i = 0 ; i < oScriptInfo.Dependency.length ; i++ )
	{
		this.Load( oScriptInfo.Dependency[i] ) ;
	}

	var sBaseScriptName = oScriptInfo.BasePath + scriptName.toLowerCase() ;

	if ( oScriptInfo.Compatibility == Jsoft_GENERIC || oScriptInfo.Compatibility == Jsoft_GENERIC_SPECIFIC )
		this._LoadScript( sBaseScriptName + '.js' ) ;

	if ( oScriptInfo.Compatibility == Jsoft_SPECIFIC || oScriptInfo.Compatibility == Jsoft_GENERIC_SPECIFIC )
	{
		if ( this._IsIE )
			this._LoadScript( sBaseScriptName + '_ie.js' ) ;
		else
			this._LoadScript( sBaseScriptName + '_gecko.js' ) ;
	}
}

JsoftScriptLoader._LoadScript = function( scriptPathFromSource )
{
	document.write( '<script type="text/javascript" src="' + this.JsofteditorPath + 'editor/_source/' + scriptPathFromSource + '"><\/script>' ) ;
}

JsoftScriptLoader.AddScript = function( scriptName, scriptBasePath, dependency, compatibility )
{
	this._Scripts[ scriptName ] =
	{
		BasePath : scriptBasePath || '',
		Dependency : dependency || [],
		Compatibility : compatibility || Jsoft_GENERIC
	} ;
}

/*
 * ####################################
 * ### Scripts Definition List
 */

JsoftScriptLoader.AddScript( 'JsoftConstants' ) ;
JsoftScriptLoader.AddScript( 'JsoftJSCoreExtensions' ) ;

JsoftScriptLoader.AddScript( 'Jsoft_Xhtml10Transitional', '../dtd/' ) ;

JsoftScriptLoader.AddScript( 'JsoftDataProcessor'	, 'classes/'	, ['JsoftConfig','JsoftBrowserInfo','JsoftRegexLib','JsoftXHtml'] ) ;
JsoftScriptLoader.AddScript( 'JsoftDocumentFragment', 'classes/'	, ['JsoftDomTools'], Jsoft_SPECIFIC ) ;
JsoftScriptLoader.AddScript( 'JsoftDomRange'		, 'classes/'	, ['JsoftBrowserInfo','JsoftJSCoreExtensions','JsoftW3CRange','JsoftElementPath','JsoftDomTools','JsoftTools','JsoftDocumentFragment'], Jsoft_GENERIC_SPECIFIC ) ;
JsoftScriptLoader.AddScript( 'JsoftDomRangeIterator', 'classes/'	, ['JsoftDomRange','JsoftListsLib'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftElementPath'		, 'classes/'	, ['JsoftListsLib'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftEnterKey'		, 'classes/'	, ['JsoftDomRange','JsoftDomTools','JsoftTools','JsoftKeystrokeHandler','JsoftListHandler'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftPanel'			, 'classes/'	, ['JsoftBrowserInfo','JsoftConfig','JsoftTools'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftImagePreloader'	, 'classes/' ) ;
JsoftScriptLoader.AddScript( 'JsoftKeystrokeHandler', 'classes/'	, ['JsoftConstants','JsoftBrowserInfo','JsoftTools'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftStyle'			, 'classes/'	, ['JsoftConstants','JsoftDomRange','JsoftDomRangeIterator','JsoftDomTools','JsoftListsLib','Jsoft_Xhtml10Transitional'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftW3CRange'		, 'classes/'	, ['JsoftDomTools','JsoftTools','JsoftDocumentFragment'], Jsoft_GENERIC ) ;

JsoftScriptLoader.AddScript( 'JsoftBrowserInfo'		, 'internals/'	, ['JsoftJSCoreExtensions'] ) ;
JsoftScriptLoader.AddScript( 'JsoftCodeFormatter'	, 'internals/' ) ;
JsoftScriptLoader.AddScript( 'JsoftConfig'			, 'internals/'	, ['JsoftBrowserInfo','JsoftConstants'] ) ;
JsoftScriptLoader.AddScript( 'JsoftDebug'			, 'internals/'	, ['JsoftConfig'] ) ;
JsoftScriptLoader.AddScript( 'JsoftDomTools'		, 'internals/'	, ['JsoftJSCoreExtensions','JsoftBrowserInfo','JsoftTools','JsoftDomRange'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftListsLib'		, 'internals/' ) ;
JsoftScriptLoader.AddScript( 'JsoftListHandler'		, 'internals/'	, ['JsoftConfig', 'JsoftDocumentFragment', 'JsoftJSCoreExtensions','JsoftDomTools'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftRegexLib'		, 'internals/' ) ;
JsoftScriptLoader.AddScript( 'JsoftStyles'			, 'internals/'	, ['JsoftConfig', 'JsoftDocumentFragment', 'JsoftDomRange','JsoftDomTools','JsoftElementPath','JsoftTools'], Jsoft_GENERIC ) ;
JsoftScriptLoader.AddScript( 'JsoftTools'			, 'internals/'	, ['JsoftJSCoreExtensions','JsoftBrowserInfo'], Jsoft_GENERIC_SPECIFIC ) ;
JsoftScriptLoader.AddScript( 'JsoftXHtml'			, 'internals/'	, ['JsoftBrowserInfo','JsoftCodeFormatter','JsoftConfig','JsoftDomTools','JsoftListsLib','JsoftRegexLib','JsoftTools','JsoftXHtmlEntities'], Jsoft_GENERIC_SPECIFIC ) ;
JsoftScriptLoader.AddScript( 'JsoftXHtmlEntities'	, 'internals/'	, ['JsoftConfig'] ) ;

// ####################################
